package br.com.fidalgotecnologia.layar.json;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import br.com.fidalgotecnologia.layar.entity.GraphicObject;
import br.com.fidalgotecnologia.layar.entity.Action;
import br.com.fidalgotecnologia.layar.entity.HotSpot;
import br.com.fidalgotecnologia.layar.entity.Layer;
import br.com.fidalgotecnologia.layar.entity.Transform;

public class JsonParser {
	public static String parseLayer(Layer layer) {
		JSONObject layarJson = new JSONObject();
		try{

			JSONArray hotSpotArray = new JSONArray();
			layarJson.put("layer", layer.getLayer());
			if(layer.getHotSpot()!=null){
				for(HotSpot hotspot: layer.getHotSpot()){
					putHotSpots(hotSpotArray, hotspot);
					
				}
			
				layarJson.put("hotspots", hotSpotArray);
			}
			layarJson.put("errorCode", layer.getErrorCode());
			layarJson.put("errorString", layer.getErrorString());
			if(layer.getNextPageKey()!=null){			
				layarJson.put("nextPageKey", layer.getNextPageKey());
			}				
			layarJson.put("morePages", layer.isMorePages());		
			layarJson.put("radius", layer.getRadius());
			layarJson.put("refreshInterval", layer.getRefreshInterval());
			layarJson.put("refreshDistance", layer.getRefreshDistance());
			layarJson.put("fullRefresh", layer.isFullRefresh());
			layarJson.put("nextPageKey", layer.getNextPageKey());
			JSONArray ActionArray = new JSONArray();
			if( layer.getActions()!=null){			
				for(Action action : layer.getActions()){
					putActions(ActionArray, action);
				}
				layarJson.put("actions", ActionArray);
			}
			layarJson.put("showMessage", layer.getShowMessage());
		}catch (Exception e) {
			e.printStackTrace();
		}
		return layarJson.toString();
	}

	private static void putHotSpots(JSONArray hotSpotArray, HotSpot hotspot)throws JSONException {
		JSONObject hotSpotJson = new JSONObject();
		JSONArray ActionArray = new JSONArray();
		hotSpotJson.put("id", hotspot.getId());
		hotSpotJson.put("distance", hotspot.getDistance());
		hotSpotJson.put("title", hotspot.getTitle());
		hotSpotJson.put("type", hotspot.getType());
		hotSpotJson.put("lat", hotspot.getLat());
		hotSpotJson.put("lon", hotspot.getLon());
		for(Action action : hotspot.getActions()){
			putActions(ActionArray, action);
		}
		hotSpotJson.put("actions",ActionArray);
		hotSpotJson.put("attribution", hotspot.getAttribution());
		hotSpotJson.put("line2",hotspot.getLine2());
		hotSpotJson.put("line3", hotspot.getLine3());
		hotSpotJson.put("line4", hotspot.getLine4());
		hotSpotJson.put("imageURL", hotspot.getImageURL());
		hotSpotJson.put("dimension", hotspot.getDimension());
		
		JSONObject objectJson = new JSONObject();
		GraphicObject object = hotspot.getObject();
		objectJson.put("baseURL",object.getBaseURL());
		objectJson.put("full",object.getFull());
		objectJson.put("reduced",object.getReduced());
		objectJson.put("icon",object.getIcon());
		objectJson.put("size",object.getSize());
		hotSpotJson.put("object", objectJson);
		
		JSONObject transformJson = new JSONObject();
		Transform transform = hotspot.getTransform();
		transformJson.put("rel",transform.isRel());
		transformJson.put("angle",transform.getAngle());
		transformJson.put("scale",transform.getScale());
		hotSpotJson.put("transform", transformJson);
		
		hotSpotJson.put("alt", hotspot.getAlt());
		hotSpotJson.put("relativeAlt", hotspot.getRelativeAlt());
		hotSpotJson.put("doNotIndex", hotspot.isDoNotIndex());
		hotSpotJson.put("inFocus", hotspot.isInFocus());
		hotSpotJson.put("showSmallBiw", hotspot.isShowSmallBiw());
		hotSpotJson.put("showBiwOnClick", hotspot.isShowBiwOnClick());
		
		hotSpotArray.put(hotSpotJson);
	}

	private static void putActions(JSONArray ActionArray, Action action)
			throws JSONException {
		JSONObject actionJson = new JSONObject();
		actionJson.put("uri", action.getUri());
		actionJson.put("label", action.getLabel());
		actionJson.put("contentType", action.getContentType());
		actionJson.put("method", action.getMethod());
		actionJson.put("params", action.getParams());
		actionJson.put("closeBiw", action.isCloseBiw());
		actionJson.put("activityType", action.getActivityType());
		actionJson.put("showActivity", action.isShowActivity());
		actionJson.put("activityMessage", action.getActivityMessage());
		actionJson.put("autoTriggerRange", action.getAutoTriggerRange());
		actionJson.put("autoTriggerOnly", action.isAutoTriggerOnly());
		ActionArray.put(actionJson);
	}
}
